const crypto = require('crypto')
const aesutil = (module.exports = {})

/**
 * @description aes加密
 * @param data 待加密内容
 * @param key 必须为32位私钥
 * @param iv 偏移量
 * @returns {string}
 */
aesutil.encryption = function (data, key, iv) {
  iv = iv || ''
  var clearEncoding = 'utf8'
  var cipherEncoding = 'hex'
  var cipherChunks = []
  var cipher = crypto.createCipheriv('aes-256-ecb', key, iv)
  cipher.setAutoPadding(true)
  cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding))
  cipherChunks.push(cipher.final(cipherEncoding))
  return cipherChunks.join('')
}

/**
 * @description aes解密
 * @param data 待解密内容
 * @param key 必须为32位私钥
 * @param iv 偏移量
 * @returns {string}
 */
aesutil.decryption = function (data, key, iv) {
  if (!data) {
    return ''
  }
  iv = iv || ''
  var clearEncoding = 'utf8'
  var cipherEncoding = 'hex'
  var cipherChunks = []
  var decipher = crypto.createDecipheriv('aes-256-ecb', key, iv)
  decipher.setAutoPadding(true)
  cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding))
  cipherChunks.push(decipher.final(clearEncoding))
  return cipherChunks.join('')
}
